package com.xd.source.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;

import com.xd.navi.NetWorkManager;
import com.xd.navi.NetworkCallbackImpl;
import com.xd.navi.OnNetStateChangeListener;

public abstract  class BaseFragment extends Fragment implements OnNetStateChangeListener {


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NetWorkManager.getInstance().addListener(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onNetStateChange(boolean isConnect) {

    }

    protected NavController nav() {
        return NavHostFragment.findNavController(this);
    }

    public void navigate(@IdRes int resId) {
        try {
            nav().navigate(resId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void navigate(@IdRes int resId, @Nullable Bundle args) {
        try {
            nav().navigate(resId, args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void navigate(@IdRes int hostResId, @IdRes int actionResId) {
        try {
            Navigation.findNavController(requireActivity(), hostResId).navigate(actionResId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void navigate(@IdRes int hostResId, @IdRes int actionResId, @Nullable Bundle args) {
        try {
            Navigation.findNavController(requireActivity(), hostResId).navigate(actionResId, args);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
